package com.dshore.messagecenter.service.topicQueue.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.dshore.messagecenter.dao.topicqueue.TopicAccessDao;
import com.dshore.messagecenter.dao.topicqueue.TopicQueueDao;
import com.dshore.messagecenter.dao.topicqueue.TopicSendDao;
import com.dshore.messagecenter.model.basedata.MdApp;
import com.dshore.messagecenter.model.system.ResponseResult;
import com.dshore.messagecenter.service.topicQueue.QueueAccessService;

@Service
public class QueueAccessServiceImpl implements QueueAccessService {

	@Autowired
	private TopicAccessDao topicAccessDao;
	@Autowired
	private TopicSendDao topicSendDao;
	@Autowired
	private TopicQueueDao topicQueueDao;
	
	/**
	 * 存取源应用
	 */
	@Override
	public Map<String, Object> saveOriApp(String originId, String originName, 
								Map<String, Object> map,HttpSession session) {

		//当session中Map有值时直接存取源应用, Map没值时创建Map存取源应用
		Map<String, String> origin = new HashMap<>();
		if(StringUtils.isNotBlank(originId)) {
			origin.put("originId", originId);
		}
		if(StringUtils.isNotBlank(originName)) {
			origin.put("originName", originName);
		}
		map.put("origin", origin);
		return map;
		
	}

	/**
	 * 存取目标应用集合
	 */
	@Override
	public ResponseResult<Map<String, Object>> saveTApp(String key, String targetId, String targetName, String index,
						 HttpSession session) {

		//Session中map
		Map<String, Object> map = (Map<String, Object>) session.getAttribute(key);
		
		//目标文件集合
		List<Map<String,String>> targetList = (List<Map<String, String>>) map.get("targetList");
		//如果session中targetList为null,再创建个list
		if(targetList == null) {
			targetList = new ArrayList<>();
		}
//		System.out.println("存取目标应用前targetList:" + targetList);
		
		// 判断目标应用是否已经被选择
//		System.out.println("传入的目标应用targetId: " + targetId);
		if(targetList.size()>0 ) {
			for(Map<String,String> tapp : targetList) {
				String appId = tapp.get("targetId");
//				System.out.println("判断appId: " + appId);
				if(appId.equals(targetId)) {
					//如果目标应用已存在，则提示
					return ResponseResult.createByError();
				}
			}
		}
		
		//方式二: 使用List删除指定下标的元素
		if(targetList.size()>0 && StringUtils.isNotBlank(index)) {
			int tAppIndex = Integer.parseInt(index);
			targetList.remove(tAppIndex);
		}
		
		
		//方式一：如果更换的目标应用id有值，就在session中的目标集合中删除(for循环找出对应的map)。
		/*if(targetList.size()>0 && StringUtils.isNotBlank(tarId)) {
			int i = 1;
			Iterator<Map<String, String>> it = targetList.iterator();
			//使用遍历删除，否则会报ConcurrrentModification异常
			while(it.hasNext()) {
				Map<String, String> tAppMap = it.next();
				System.out.println("第" + i + "次遍历targetList: " + targetList);
				
				String tappId = tAppMap.get("targetId");
				System.out.println("tappId: " + tappId);
				//目标应用id若相同删除该id对应的map
				if(tappId.equals(tarId)) {
					//必须使用iterator的remove()方法删除元素，否则异常
					it.remove();
				}
				System.out.println("第" + i + "次删除之后的targetList: " + targetList);
				i++;
			}
		}*/
		//单个目标应用targetMap
		Map<String, String> tApp = new HashMap<>();
		if(StringUtils.isNotBlank(targetId)) {
			tApp.put("targetId", targetId);
		}
		if(StringUtils.isNotBlank(targetName)) {
			tApp.put("targetName", targetName);
		}
		//将单个目标应用存入list集合中
		targetList.add(tApp);
//		System.out.println("存取目标应用后集合: " + targetList);
		
		
		//将目标应用集合存入map中
		map.put("targetList", targetList);
//		System.out.println("存储完目标应用后的map： " + map);
		//将map再更新存入Session中
		session.setAttribute(key, map);

		return ResponseResult.createBySuccess(map);
	}

	
	/**
	 * 修改时获取应用数据
	 */
	@Override
	public Map<String, Object> getQueueInfo(String topicQueueNo, String id) {
		//初始化Session中Map
		Map<String, Object> map = new HashMap<>();
		
		/**根据id主键查询主题队列名称*/
		String topicQueueName = topicQueueDao.queryTQName(id);
//		System.out.println("修改时数据库中主题队列名称: " + topicQueueName);
		map.put("topicQueueName", topicQueueName);
		
		
		//1)接入信息(根据topicQueueNo获取)
		List<MdApp> oriAppList = topicAccessDao.queryOriAppByQueueSign(topicQueueNo);
		//查询1个源应用并存入session
		if(oriAppList.size() > 0) {
			MdApp oriApp = oriAppList.get(0);
			
			Map<String, String> origin = new HashMap<>();
			if(StringUtils.isNotBlank(oriApp.getId())) {
				origin.put("originId", oriApp.getId());
			}
			if(StringUtils.isNotBlank(oriApp.getAppName())) {
				origin.put("originName", oriApp.getAppName());
			}
			map.put("origin", origin);
		}
		
		//2)发送信息(根据topicQueueNo获取)
		List<MdApp> targetInfos = topicSendDao.queryTargetAppByQueueSign(topicQueueNo);
		//目标文件集合
		List<Map<String,String>> targetList = new ArrayList<>();
		//查询多个目标应用并存入session
		if(targetInfos.size() > 0) {
			for(MdApp tApp : targetInfos) {
				//tApps为单个目标应用信息
				Map<String, String> tAppMap = new HashMap<>();
				if(StringUtils.isNotBlank(tApp.getId())) {
					tAppMap.put("targetId", tApp.getId());
				}
				if(StringUtils.isNotBlank(tApp.getAppName())) {
					tAppMap.put("targetName", tApp.getAppName());
				}
				//将单个目标应用存入list集合中
				targetList.add(tAppMap);
				
			}
		}

//		System.out.println("存取目标应用后集合: " + targetList);
		//将目标文件集合存入map中
		map.put("targetList", targetList);
		return map;
	}

	/**
	 * 根据下标删除目标应用
	 */
	@Override
	public void delTarApp(int idIndex, Map<String, Object> map) {
		//目标文件集合
		List<Map<String,String>> targetList = (List<Map<String, String>>) map.get("targetList");
//		System.out.println("删除前targetList： " + targetList);
		
		//使用List删除指定下标的元素
		targetList.remove(idIndex);
//		System.out.println("删除后targetList： " + targetList);
	}

}
